package com.lzc.tlmail.cart.tlmailcart.controller;

import com.lzc.tlmail.cart.tlmailcart.interceptor.CartInterceptor;
import com.lzc.tlmail.cart.tlmailcart.service.CartService;
import com.lzc.tlmail.cart.tlmailcart.to.UserInfoto;
import com.lzc.tlmail.cart.tlmailcart.vo.CartItemVo;
import com.lzc.tlmail.cart.tlmailcart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author lzc
 * @create 2020-10-18 14:40
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 改变购物车数量
     * @return
     */
    @GetMapping("/cartNumChange")
    public String cartNumChange(@RequestParam("count")Integer count,@RequestParam("skuId")Long skuId){
        cartService.cartNumChange(count,skuId);
        return "redirect:http://www.cart.tlmail.com/cart.html";
    }

    /**
     * 改变选中商品项的状态
     * @param checked
     * @param skuId
     * @return
     */
    @GetMapping("/cartCheck")
    public String cartCheck(@RequestParam("isChecked")Integer checked,@RequestParam("skuId")Long skuId){
        cartService.cartCheck(checked,skuId);
        return "redirect:http://www.cart.tlmail.com/cart.html";
    }

    /**
     * 跳转到购物车页面
     * @return
     */
    @GetMapping("/cart.html")
    public String toCart(Model model) throws ExecutionException, InterruptedException {
      CartVo cartVo=cartService.getCart();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    /**
     * 添加到购物车
     * @return
     */
    @RequestMapping("/addCart")
    public String addCart(@RequestParam("skuNum")Integer skuNum,
                          @RequestParam("skuId")Long skuId,
                          RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        cartService.addCart(skuNum,skuId);
       redirectAttributes.addAttribute("skuId",skuId);
        return "redirect:http://www.cart.tlmail.com/addCartSuccess";
    }

    /**
     * 添加购物车到成功页面
     * @return
     */
    @GetMapping("/addCartSuccess")
    public String addCartSuccess(@RequestParam("skuId")Long skuId,Model model){
      CartItemVo cartItemVo=cartService.getCartItemBySkuId(skuId);
        model.addAttribute("cartItem",cartItemVo);
        return "success";
    }

    /**
     * 删除购物车
     * @return
     */
    @GetMapping("/skuDel")
    public String skuDel(@RequestParam("skuId")Long skuId){
        cartService.skuDel(skuId);
        return "redirect:http://www.cart.tlmail.com/cart.html";
    }


    /**
     * 获取购物项
     */
    @ResponseBody
    @GetMapping("/getOrderCartItems")
    public List<CartItemVo> getCartItems(){

        return cartService.getCartItemsToOrder();
    }
}
